<?php
namespace app\index\controller;

use app\index\model\Brand;
use app\index\model\Cart;
use app\index\model\Goods;
use app\index\model\Order;
use think\Controller;
use think\View;

class Index extends Controller
{
    public function index(){

        $b = new Brand();
        $brand = $b->with('goods')->select();

        $g=new Goods();
        $goods = $g->all();


        $view = new View();
        $view->assign('brand',$brand);
        $view->assign('goods',$goods);
        return $view->fetch('index');
    }

    public function details($goods_id=''){
        $b = new Brand();
        $brand = $b->with('goods')->select();

        $g = new Goods();
        $goods_details = $g->get($goods_id);

        $view = new View();
        $view->assign('brand',$brand);
        $view->assign('details',$goods_details);
        return $view->fetch('details');
    }

    public function cart(){
        $c =new Cart();
        $cart = $c->where('user_id',1)->order('id','desc')->select(1);

        $view = new View();
        $view->assign('cart',$cart);

        echo $view->fetch('carts');
    }

    public function ajaxReturn(){

        $id = input('post.id');
        $num = input('post.num');
        $c = new Cart();
        $result = $c->where('id',$id)
                    ->update([
                        'goods_num'=> $num,
                    ]);
        if($result){
            $array = array(
                "info"=>'修改成功',
                'status' => 1,
            );
        }else{
            $array = array(
                'info' => '修改失败'
            );
        }
        echo json_encode($array);
    }


    public function jiesuan(){
        $c =new Cart();
        $cart = $c->where('user_id',1)->order('id','desc')->select(1);

        $view = new View();
        $view->assign('cart',$cart);

        return $view->fetch('jiesuan');
    }

    public function pay(){
        $o = new Order();
        $order = $o->where('user_id',session('userid'))->order('add_time','desc')->find();
        $paybeforetime = date("Y-m-d",strtotime("+1 day",strtotime($order['add_time'])));
        $view = new View();
        $view->assign('paybeforetime',$paybeforetime);
        $view->assign('order',$order);
        return $view->fetch('pay');
    }


    public function order_add(){
        
        $user_id = input('post.userid');
        $consignee = input('post.consignee');
        $address = input('post.address');
        $mobile = input('post.mobile');
        $goods_name=input('post.goods_name');
        $goods_price=input('post.goods_price');
        $market_price=input('post.market_price');
        $goods_num=input('post.goods_num');

        $o = new Order();
        $c = new Cart();

        $o->data(array(
            'order_sn' => $c->get_order_sn(),
            'user_id' => $user_id,
            'consignee' => $consignee,
            'address' => $address,
            'mobile' => $mobile,
            'goods_name' => $goods_name,
            'goods_price' => $goods_price,
            'market_price' => $market_price,
            'goods_num' => $goods_num,
            'add_time' => date('Y-m-d H:i:s'),
        ));

        $result = $o->save();
        if($result){
            $this->success('添加订单成功，请尽快付款','index/index/pay');

        }else{
            $this->error('添加订单失败，请重新操作','index/index/jiesuan');
        }


    }

}
